

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="表达式中包含运算对象、运算符和圆括号等，习惯上使用中缀表示（指运算符夹在两运算符对象中间）形式。为了直接指明表达式中各运算对象的先后计算顺序，可将表达式的中缀形式转换成后缀（指运算符放在二运算对象的后面）形式。">
<meta property="og:type" content="article">
<meta property="og:title" content="表达式的后缀表示">
<meta property="og:url" content="https://cheney822.gitee.io/2020/12/02/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%AE%9E%E9%AA%8C--%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E5%90%8E%E7%BC%80%E8%A1%A8%E7%A4%BA/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="表达式中包含运算对象、运算符和圆括号等，习惯上使用中缀表示（指运算符夹在两运算符对象中间）形式。为了直接指明表达式中各运算对象的先后计算顺序，可将表达式的中缀形式转换成后缀（指运算符放在二运算对象的后面）形式。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/e5fc090c68954a759a92a4a9587e53d0.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/0dda8b0123bd4b13935d9faaa9181e56.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/257f7ca4cda2441d96ce44a89ff2f4ab.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/7c22f9252afb494bb388b45b62f6d9e7.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/ecd30945104b4631bb727525c10a1318.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/32b4c53901e14cdeab97342e1da0e914.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/075ad35c1ac748e69818588fab2d7d6d.png">
<meta property="article:published_time" content="2020-12-02T05:14:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:28.003Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="栈">
<meta property="article:tag" content="后缀">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/e5fc090c68954a759a92a4a9587e53d0.png">
  
  
  <title>表达式的后缀表示 - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="表达式的后缀表示">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-02 13:14" pubdate>
        2020年12月2日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      36 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">表达式的后缀表示</h1>
            
            <div class="markdown-body">
              <h1 id="一、问题描述"><a href="#一、问题描述" class="headerlink" title="一、问题描述"></a>一、问题描述</h1><p>表达式中包含运算对象、运算符和圆括号等，习惯上使用中缀表示（指运算符夹在两运算符对象中间）形式。计算表达式的值，涉及到运算符的优先级别，如先乘除后加减。括在一对圆括号中的子表达式必须先计算，因此，圆括号可视为特殊的运算符，具有最高优先级别。圆括号可以任意嵌套，这意味着左圆括号后面又是表达式，形成表达式的递归定义。</p>
<p>为了直接指明表达式中各运算对象的先后计算顺序，可将表达式的中缀形式转换成后缀（指运算符放在二运算对象的后面）形式。例如，表达式a<em>b-(c+d)/e，这是通常的中缀形式，其后缀表示是ab</em>cd+e/-,其中圆括号在后缀形式中已不见了。设计一转换程序，将输入的任一表达式转换成相应的后缀形式后输出。</p>
<p>【基本要求】<br>为简单起见，假定运算对象只含变量，且每个变量名以单字母表示；运算符仅含+、-、*、/和圆括号；表达式以分号“；”结尾。在转换过程中，要求作必要的语法检查，例如圆括号是否配对，单词是否合法等。要求分别编写转换程序的非递归与递归算法。</p>
<p>【测试数据】<br>（1）A<em>B</em>C<br>（2）a+b*(c-d)-e/f<br>（3）(A+B)<em>D+E/(F+A</em>D)+C</p>
<h1 id="二、需求分析"><a href="#二、需求分析" class="headerlink" title="二、需求分析"></a>二、需求分析</h1><p>要解决的问题:要表达一个算式，通常有三种形式即：前缀、中缀和后缀。其中人们最常用的最便于人理解的就是中缀表达式，但是其不便于计算器进行计算，计算器更擅长的是计算后缀表达式，因此我们需要设计一个算法来把一个已知的中缀表达式转化成后缀形式。</p>
<p>程序的功能：键盘键入一个中缀表达式，系统自动输出其对应的后缀形式。</p>
<p>输入和输出的形式：输入表达式运算符包括（数字（整数和小数）、字母（大写和小写））操作符（加、减、乘、除、括号）。输出时，不同的操作符和运算符之间以空格分开，防止歧义。
 </p>
<h1 id="三、设计"><a href="#三、设计" class="headerlink" title="三、设计"></a>三、设计</h1><h2 id="3-1-设计思想"><a href="#3-1-设计思想" class="headerlink" title="3.1 设计思想"></a>3.1 设计思想</h2><p>数据结构设计:<br>本程序主要采用顺序栈作为数据结构，得益于其后进先出的特性，对表达式的转化问题就可以转化为不同优先级符号的进出栈问题。由于本程序所操作的对象均为比较小的表达式且没有过多的插入删除操作，故采用顺序栈储存操作符，用数组储存中缀表达式。</p>
<p>算法设计:</p>
<p><img src="https://imgbed.cheney.cc/picgo/e5fc090c68954a759a92a4a9587e53d0.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br> <img src="https://imgbed.cheney.cc/picgo/0dda8b0123bd4b13935d9faaa9181e56.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p> </p>
<h2 id="3-2-详细设计"><a href="#3-2-详细设计" class="headerlink" title="3.2 详细设计"></a>3.2 详细设计</h2><h3 id="首先在CCStack-h自定义一个Mystack类模板："><a href="#首先在CCStack-h自定义一个Mystack类模板：" class="headerlink" title="首先在CCStack.h自定义一个Mystack类模板："></a>首先在CCStack.h自定义一个Mystack类模板：</h3><p>数据成员 data数组用来存数据，topn用来指示栈顶的位置</p>
<p>函数：初始化函数init用来初始化栈顶为0号位置；empty用来判断栈是否为空，空则返回true；top函数返回栈顶元素，不出栈；push（x）函数用于将x进栈；pop用于将栈顶出栈。<br>在Postfix1.cpp引用CCStack.h并定义以下函数：</p>
<blockquote>
<p>isOperator用来判断一个字符是否为加减乘除，是则返回true<br>isOperands用来判断一个字符是否为操作数(数字+大小写字母+小数点)，是则返回true<br>GetPriority用来返回字符在计算中的优先级：#为零、（为1、+-为2、*/为3。<br>Calculater用非递归的方法转换表达式，<br>D_Calculater用非递归的方法转换表达式。</p>
</blockquote>
<h3 id="转换的原理："><a href="#转换的原理：" class="headerlink" title="转换的原理："></a>转换的原理：</h3><p>从头开始扫描存储表达式的字符数组arr，直到arr[i] = ‘=’及扫描到尾部的等于号为止，定义op栈用来存储转换过程中的操作符，首先把优先级最低的#压入栈用来判断栈底位置。</p>
<p><strong>判断arr[i]的类型：</strong><br>1.如果是数字，就直接输出，并且设置一个循环判断下一位是否为数字（确保大于一位的数字以及小数能够正确输出而不被拆开）如果是则接着输出，然后输出一个空格表示当前的数字已经输出完毕<br>2.如果是左括号，就直接压入栈<br>3.如果是右括号，设置循环一直推出栈中已有的操作符直到遇到左括号，并把左括号也推出<br>4.如果是运算符（+-*/四种）设置循环，只要#不是栈顶（栈不空），用当前arr[i]和栈顶比较优先级，如果优先级低或等于栈顶则直接输出，如果高于栈顶则则入栈<br>主循环至此已经结束<br>继续判断栈顶是否为#，若不是则一直输出操作符，直到#为栈顶</p>
<h3 id="递归的算法："><a href="#递归的算法：" class="headerlink" title="递归的算法："></a>递归的算法：</h3><p>总体思想与非递归类似，但是用递归调用D_Calculater的方式代替循化，每次判断传入的参数arr[0]，每次递归调用时传递的参数为当前数组第二个元素（1号位）的地址<br>递归出口为：arr[0]为等号，算法进行到这一步，若栈中仍有操作符则依次全部出栈</p>
<h2 id="格式检查："><a href="#格式检查：" class="headerlink" title="格式检查："></a>格式检查：</h2><p>在非递归算法中，在判断完所有的类型均不符合后，直接当作非法字符 。在递归算法中，进行比较之前第一步用其与支持的所有字符比较，若均不相等，则视为非法字符。<br>对括号的检查直接在对右括号的操作语句中进行，即判断是右括号即将输出栈中左括号之前所有符号时，判断栈顶是否为#，若是则等号不匹配</p>
<h3 id="main函数："><a href="#main函数：" class="headerlink" title="main函数："></a>main函数：</h3><p>在 main函数中定义一个栈Oper用来存储递归方式下的操作符，并在其底部插入#作为判断的标志，在提示键盘键入 一个表达式之后，输入需要转化的表达式（以=结尾），用std::cin.getline(x, 250);将表达式读入到字符数组x中，单独输入0作为结束的标志，然后分别把x和Oper作为参数传入两个函数中即Calculater(x);和D_Calculater(x, Oper);
 </p>
<h1 id="四、代码实现："><a href="#四、代码实现：" class="headerlink" title="四、代码实现："></a>四、代码实现：</h1><figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp">﻿<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;CCStack.h&quot;</span></span><br><span class="hljs-comment">//#include &lt;stack&gt;</span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><br><span class="hljs-comment">// 判断是否是操作符</span><br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">isOperator</span><span class="hljs-params">(<span class="hljs-type">char</span> ch)</span> </span>&#123;<br>    <span class="hljs-keyword">if</span> (ch == <span class="hljs-string">&#x27;+&#x27;</span> || ch == <span class="hljs-string">&#x27;-&#x27;</span> || ch == <span class="hljs-string">&#x27;*&#x27;</span> || ch == <span class="hljs-string">&#x27;/&#x27;</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>&#125;<br><br><span class="hljs-comment">//判断是否为操作数(数字+字母+小数点)</span><br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">isOperands</span><span class="hljs-params">(<span class="hljs-type">char</span> ch)</span> </span>&#123;<br>    <span class="hljs-keyword">if</span> (ch &lt;= <span class="hljs-string">&#x27;9&#x27;</span> &amp;&amp; ch &gt;= <span class="hljs-string">&#x27;0&#x27;</span> || ch &lt;= <span class="hljs-string">&#x27;Z&#x27;</span> &amp;&amp; ch &gt;= <span class="hljs-string">&#x27;A&#x27;</span> || ch &lt;= <span class="hljs-string">&#x27;z&#x27;</span> &amp;&amp; ch &gt;= <span class="hljs-string">&#x27;a&#x27;</span> || ch == <span class="hljs-string">&#x27;.&#x27;</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>&#125;<br><br><span class="hljs-comment">// 获取优先级</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">GetPriority</span><span class="hljs-params">(<span class="hljs-type">char</span> ch)</span> </span>&#123;<br>    <span class="hljs-type">int</span> Rank = <span class="hljs-number">0</span>; <span class="hljs-comment">// 优先级</span><br><br>    <span class="hljs-keyword">switch</span> (ch) &#123;<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;#&#x27;</span>:<br>        Rank = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;(&#x27;</span>:<br>        Rank = <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;+&#x27;</span>:<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;-&#x27;</span>:<br>        Rank = <span class="hljs-number">2</span>;<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;*&#x27;</span>:<br>    <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;/&#x27;</span>:<br>        Rank = <span class="hljs-number">3</span>;<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">default</span>:<br>        <span class="hljs-keyword">break</span>;<br>    &#125;<br>    <span class="hljs-keyword">return</span> Rank;<br>&#125;<br><br><br><span class="hljs-comment">//非递归算法</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">Calculater</span><span class="hljs-params">(<span class="hljs-type">char</span> arr[])</span> </span>&#123;<br>    <br>    <span class="hljs-type">char</span> N; <span class="hljs-comment">// 用来依次读取从栈中取出的操作符</span><br>    MyStack&lt;<span class="hljs-type">char</span>&gt; Op; <span class="hljs-comment">// 栈op:存储操作符</span><br>    Op.<span class="hljs-built_in">push</span>(<span class="hljs-string">&#x27;#&#x27;</span>);<span class="hljs-comment">//在头部插入#，用来判读栈底</span><br>   <span class="hljs-comment">// for (int i = 0; i &lt; (int)strlen(arr);) &#123;//主循环，逐字符判断 </span><br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; arr[i] != <span class="hljs-string">&#x27;=&#x27;</span>;) &#123;<span class="hljs-comment">//主循环，逐字符判断 </span><br>        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isOperands</span>(arr[i])) &#123; <span class="hljs-comment">// 如果是数字 就直接出栈 并把连续的数字组成一个完整的数输出 用空格分开</span><br>            <span class="hljs-keyword">for</span> (; <span class="hljs-built_in">isOperands</span>(arr[i]); i++) std::cout &lt;&lt; arr[i];<br>            std::cout &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>        &#125;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[i] == <span class="hljs-string">&#x27;(&#x27;</span>) &#123; <span class="hljs-comment">// 如果是左括号（ 就直接进栈</span><br>            Op.<span class="hljs-built_in">push</span>(arr[i]);<br>            i++;<br>        &#125;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isOperator</span>(arr[i])) &#123; <span class="hljs-comment">// 判断是否为操作符操作符</span><br>            <span class="hljs-keyword">while</span> (Op.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;#&#x27;</span>) &#123;<br>                N = Op.<span class="hljs-built_in">top</span>();<br>                <span class="hljs-keyword">if</span> (<span class="hljs-built_in">GetPriority</span>(arr[i]) &lt;= <span class="hljs-built_in">GetPriority</span>(N)) &#123;<br>                    <span class="hljs-comment">// 优先级低或等于栈顶则直接输出</span><br>                    std::cout &lt;&lt; N &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>                    Op.<span class="hljs-built_in">pop</span>();<br>                &#125;<br>                <span class="hljs-keyword">else</span> <span class="hljs-comment">// ch优先级高于栈中操作符</span><br>                    <span class="hljs-keyword">break</span>;<br>            &#125;<br>            Op.<span class="hljs-built_in">push</span>(arr[i]); <span class="hljs-comment">// 入栈</span><br>            i++;<br>        &#125;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[i] == <span class="hljs-string">&#x27;)&#x27;</span>) &#123; <span class="hljs-comment">// 如果是右括号，一直推出栈中操作符，直到遇到左括号(</span><br>            <span class="hljs-keyword">while</span> (Op.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;(&#x27;</span>) &#123;<br>                <span class="hljs-keyword">if</span> (Op.<span class="hljs-built_in">top</span>() == <span class="hljs-string">&#x27;#&#x27;</span>) &#123; std::cout &lt;&lt; <span class="hljs-string">&quot;  括号不匹配  &quot;</span>; <br>                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>                &#125;<br>                std::cout &lt;&lt; Op.<span class="hljs-built_in">top</span>() &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>                Op.<span class="hljs-built_in">pop</span>();<br>            &#125;<br>        <br>            Op.<span class="hljs-built_in">pop</span>(); <span class="hljs-comment">// 左括号出栈</span><br>            i++;<br>        &#125;<br>        <span class="hljs-keyword">else</span> &#123;<br>            std::cout &lt;&lt; <span class="hljs-string">&quot;  非法字符！！！  &quot;</span>;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>    &#125; <span class="hljs-comment">//主循环for结束(扫描结束)</span><br><br>    <span class="hljs-keyword">while</span> ( Op.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;#&#x27;</span>) &#123; <span class="hljs-comment">// 当栈不空，继续输出操作符</span><br>        std::cout &lt;&lt; Op.<span class="hljs-built_in">top</span>() &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>        Op.<span class="hljs-built_in">pop</span>();<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>&#125;<br><br><br><span class="hljs-comment">//递归算法</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">D_Calculater</span><span class="hljs-params">(<span class="hljs-type">char</span> arr[], MyStack&lt;<span class="hljs-type">char</span>&gt; Op1)</span> </span>&#123;<br><br>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>])==<span class="hljs-string">&#x27;(&#x27;</span>&amp;&amp; <span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>]) != <span class="hljs-string">&#x27;)&#x27;</span>&amp;&amp;<span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>]) != <span class="hljs-string">&#x27;=&#x27;</span>&amp;&amp;!<span class="hljs-built_in">isOperands</span>(<span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>])) &amp;&amp; !<span class="hljs-built_in">isOperator</span>(<span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>])))&#123;<br>        std::cout &lt;&lt; <span class="hljs-string">&quot;  非法字符！！！  &quot;</span>;<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isOperands</span>(arr[<span class="hljs-number">0</span>])) &#123;<br>        <span class="hljs-type">int</span> i = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (; <span class="hljs-built_in">isOperands</span>(arr[i]); i++) std::cout &lt;&lt; arr[i];<br>        std::cout &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>        <span class="hljs-built_in">D_Calculater</span>(&amp;arr[i], Op1);<br>    &#125;<br><br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[<span class="hljs-number">0</span>] == <span class="hljs-string">&#x27;(&#x27;</span>) &#123;<br>        Op1.<span class="hljs-built_in">push</span>(arr[<span class="hljs-number">0</span>]);<br>        <span class="hljs-built_in">D_Calculater</span>(&amp;arr[<span class="hljs-number">1</span>], Op1);<br>    &#125;<br><br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-built_in">isOperator</span>(arr[<span class="hljs-number">0</span>])) &#123;<br>        <span class="hljs-keyword">while</span> (Op1.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;#&#x27;</span>) &#123;<br>            <span class="hljs-type">char</span> c = Op1.<span class="hljs-built_in">top</span>();<br>            <span class="hljs-keyword">if</span> (<span class="hljs-built_in">GetPriority</span>(arr[<span class="hljs-number">0</span>]) &lt;= <span class="hljs-built_in">GetPriority</span>(c)) &#123;<br>                <span class="hljs-comment">// 优先级低或等于</span><br>                std::cout &lt;&lt; c &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>                Op1.<span class="hljs-built_in">pop</span>();<br>            &#125;<br>            <span class="hljs-keyword">else</span> <span class="hljs-comment">// ch优先级高于栈中操作符</span><br>                <span class="hljs-keyword">break</span>;<br>        &#125; <span class="hljs-comment">// while结束</span><br>        Op1.<span class="hljs-built_in">push</span>(arr[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 防止不断的推出操作符，最后空栈了;或者ch优先级高了</span><br>        <span class="hljs-built_in">D_Calculater</span>(&amp;arr[<span class="hljs-number">1</span>], Op1);<br>    &#125;<br><br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[<span class="hljs-number">0</span>] == <span class="hljs-string">&#x27;)&#x27;</span>) &#123;<br>        <span class="hljs-keyword">while</span> (Op1.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;(&#x27;</span>) &#123;<br>            <span class="hljs-keyword">if</span> (Op1.<span class="hljs-built_in">top</span>() == <span class="hljs-string">&#x27;#&#x27;</span>) &#123;<br>                std::cout &lt;&lt; <span class="hljs-string">&quot;  括号不匹配  &quot;</span>;<br>                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>            &#125;<br>            std::cout &lt;&lt; Op1.<span class="hljs-built_in">top</span>() &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<br>            Op1.<span class="hljs-built_in">pop</span>();<br>        &#125;<br>        Op1.<span class="hljs-built_in">pop</span>(); <span class="hljs-comment">// 左括号出栈</span><br>        <span class="hljs-built_in">D_Calculater</span>(&amp;arr[<span class="hljs-number">1</span>], Op1);<br>    &#125;<br><br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arr[<span class="hljs-number">0</span>] == <span class="hljs-string">&#x27;=&#x27;</span> &amp;&amp; Op1.<span class="hljs-built_in">top</span>() != <span class="hljs-string">&#x27;#&#x27;</span>) &#123; <span class="hljs-comment">//递归出口（扫描到#）</span><br>        std::cout &lt;&lt; Op1.<span class="hljs-built_in">top</span>() &lt;&lt; <span class="hljs-string">&quot; &quot;</span>;<span class="hljs-comment">//输出栈中剩下的元素</span><br>        Op1.<span class="hljs-built_in">pop</span>();<br>        <span class="hljs-built_in">D_Calculater</span>(&amp;arr[<span class="hljs-number">0</span>], Op1);<span class="hljs-comment">//确保栈中元素输出完（可用循环代替）</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>    &#125;<br><br><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>&#125; <br><br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>    std::cout &lt;&lt; <span class="hljs-string">&quot;\n请输入要转换的中缀表达式(以=结尾)\n&quot;</span>;<br>   <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>    std::cout &lt;&lt; <span class="hljs-string">&quot;\n------------Begin------------\n&quot;</span>;<br>    std::cout &lt;&lt; <span class="hljs-string">&quot;表达式：&quot;</span>;<br>    <span class="hljs-type">char</span> x[<span class="hljs-number">250</span>];<br>    MyStack&lt;<span class="hljs-type">char</span>&gt; Oper; <span class="hljs-comment">// 栈oper:存储递归方式下的操作符</span><br>    Oper.<span class="hljs-built_in">push</span>(<span class="hljs-string">&#x27;#&#x27;</span>);<span class="hljs-comment">//在头部插入#，用来判读栈底</span><br>    std::cin.<span class="hljs-built_in">getline</span>(x, <span class="hljs-number">250</span>);<span class="hljs-comment">//将读到的表达书存入字符数组x，格式(接收字符串的变量,接收字符个数)</span><br>    <span class="hljs-keyword">if</span>(x[<span class="hljs-number">0</span>]==<span class="hljs-string">&#x27;0&#x27;</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<span class="hljs-comment">//输入0 退出系统</span><br>    std::cout &lt;&lt; <span class="hljs-string">&quot;\n非递归转换后的后缀表达式为：   &quot;</span>;<br>    <span class="hljs-built_in">Calculater</span>(x);<br>    std::cout &lt;&lt; <span class="hljs-string">&quot;\n  递归转换后的后缀表达式为：   &quot;</span>;<br>    <span class="hljs-built_in">D_Calculater</span>(x, Oper);<br>    std::cout &lt;&lt; <span class="hljs-string">&quot;\n-------------End-------------\n&quot;</span>;<br>   &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>


<h1 id="五、用户手册："><a href="#五、用户手册：" class="headerlink" title="五、用户手册："></a>五、用户手册：</h1><p>打开系统后，待弹出输入要转换的中缀表达式提示后，开始键入表达式。<br>格式要求为：1.必须以等号结尾 2.必须是正确的表达式 3.支持的字符有数字0-9、大小写字母、小数点、小括号、加减乘除、等于号 4.以上所有字符均为英文下的字符。<br>输入完表达式后，单击回车即可计算，系统自动显示用递归与非递归方法计算出的后缀表达式，然后可以再次输入其他表达式。<br>若输入的表达式括号不匹配或者存在非法字符，则直接结束本次计算，输入单个数字0可以关闭软件。</p>
<h1 id="六、测试数据及测试结果："><a href="#六、测试数据及测试结果：" class="headerlink" title="六、测试数据及测试结果："></a>六、测试数据及测试结果：</h1><p>测试输入：5+8*(9.3-1)-4/9=<br>测试目的：设计该输入的目的在于测试程序能否处理含整数和小数的表达式；<br>正确输出：5 8 9.3 1 - * + 4 9 / -<br>实际输出：5 8 9.3 1 - * + 4 9 / -<br> <img src="https://imgbed.cheney.cc/picgo/257f7ca4cda2441d96ce44a89ff2f4ab.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>测试输入：(A+B)<em>D+E/(F+A</em>D)+C=<br>测试目的：设计该输入的目的在于测试程序在哪方面可能存在漏洞；<br>正确输出： A B + D * E F A D * + / + C +<br>实际输出： A B + D * E F A D * + / + C +      </p>
<p><img src="https://imgbed.cheney.cc/picgo/7c22f9252afb494bb388b45b62f6d9e7.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>测试输入：0<br>测试目的：设计该输入的目的在于测试程序能否按照预期的停下来；<br>正确输出：程序终止<br>实际输出：程序终止      </p>
<p><img src="https://imgbed.cheney.cc/picgo/ecd30945104b4631bb727525c10a1318.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p> <br>测试输入：88*9)=<br>测试目的：设计该输入的目的在于测试程序能否检测括号是否匹配；<br>正确输出：违法括号之前的正确后缀+“括号不匹配”<br>实际输出：违法括号之前的正确后缀+“括号不匹配” </p>
<p> <img src="https://imgbed.cheney.cc/picgo/32b4c53901e14cdeab97342e1da0e914.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>测试输入：[]<br>测试目的：设计该输入的目的在于测试程序能否检测违法字符；<br>正确输出：非法字符!!!<br>实际输出：非法字符!!!<br><img src="https://imgbed.cheney.cc/picgo/075ad35c1ac748e69818588fab2d7d6d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/C/">C++</a>
                    
                      <a class="hover-with-bg" href="/tags/%E6%A0%88/">栈</a>
                    
                      <a class="hover-with-bg" href="/tags/%E5%90%8E%E7%BC%80/">后缀</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/03/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%AE%9E%E9%AA%8C--%E5%A4%A7%E6%95%B0%E8%BF%90%E7%AE%97%E4%B9%8B%E8%AE%A1%E7%AE%97n%E7%9A%84%E9%98%B6%E4%B9%98%20(n%E2%89%A520)/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">大数运算之计算n的阶乘 (n≥20)</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/01/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E8%AF%BE%E8%AE%BE--%E6%B4%9B%E8%B0%B7P048%E7%96%AB%E6%83%85%E6%8E%A7%E5%88%B6/">
                        <span class="hidden-mobile">洛谷P048疫情控制</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
